/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Máté
 */
public class Reader {           //A halozat-leiras beolvasasa

    private static Reader instance;     //egy peldanyt hozunk letre
    private String description;         // a leirast egy stringben taroljuk
    private String output;             // kimeneti string

    private Reader() {
        this.description = "";      //konstruktor
        this.output = "";
    }

    public void resetDescription() {       //seged metodusok
        description = "";
    }
    public void resetOutuput() {
        output = "";
    }
    
    //leiras beolvasasa filebol
    public void readDescription(String path) throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        StringBuilder builder = new StringBuilder();
        String line;            

        while ((line = reader.readLine()) != null) {    //feldolgozzuk a file-t soronként
            builder.append(line);
            builder.append("\n");
        }

        description = builder.toString();   //stringge konvertalunk
        reader.close();                     //bezarjuk a filet
    }

    public void buildDescription(String command) {     //a bevitt parancsokat hozzaadjuk a lerashoz
        description = description.concat(command);
        description = description.concat("\n");
    }

    public void writeDescription(String path) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));

        writer.write(description);  //a teljes szoveges leirast az eredmenyekkel egyutt kiirjuk file-ba

        writer.close();
    }

    public void buildOutput(String value) {   //kimeneti elemek altal produkalt ertekeket adunk a leirashoz
        output = output.concat(value);
        output = output.concat("\n");
    }

    public void writeOutput(String path) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
            // a path eleresi uttal adott file-be irjuk az output erteket
        writer.write(output);

        writer.close();
    }

    public String getDescription() {    //seged metodusok
        return description;
    }

    public static Reader getInstance() {
        if (instance == null) {
            instance = new Reader();
        }
        return instance;
    }
}
